﻿using UnityEngine;
using System.Collections;

public class UtilManager : SingletonMonoBehaviour<UtilManager> {



	//==================================================================
	// alpha animation
	//==================================================================

	UISprite[] AlphaTargetUISprite;
	UILabel[]  AlphaTargetUILabel;


	//public bool PlayStartAlphaFlag = false;
	//public bool RePlayStartAlphaFlag = false;


	//[SerializeField]
	//private GameObject[] UpperWall = new GameObject[5];
	//Color wallAlpha;

	/*[SerializeField]
	private GameObject L_BlackWall;
	[SerializeField]
	private GameObject R_BlackWall;
	*/

	/**
	 * NGUIのUISpriteとUILabelに対してアルファフェードインする
	 */
	public void StartNGUIAlphaAnim(GameObject target, float animTime, float from=0, float to=1)
	{
		AlphaTargetUISprite = target.GetComponentsInChildren<UISprite> ();
		AlphaTargetUILabel = target.GetComponentsInChildren<UILabel> ();

		updateAlphaAnim( from );

		iTween.ValueTo (
			gameObject,
			iTween.Hash (
				"from",		from,
				"to",		to,
				"time",		animTime,
				"onupdate",	"updateAlphaAnim",
				"easetype",	iTween.EaseType.easeInSine
			)
		);
	}

	/*
	public void ResultNGUIAlphaAnim(GameObject target, float animTime)
	{
		AlphaTargetUISprite = target.GetComponentsInChildren<UISprite> ();
		AlphaTargetUILabel = target.GetComponentsInChildren<UILabel> ();
		
		updateAlphaAnim( 0 );
		
		
		iTween.ValueTo (
			gameObject,
			iTween.Hash (
			"from",		0,
			"to",		1,
			"time",		animTime,
			"delay",	3.5f,
			"onupdate",	"updateAlphaAnim",
			"easetype",	iTween.EaseType.linear
			)
			);
	}*/


	void updateAlphaAnim(float val)
	{
		foreach (var tmp in AlphaTargetUISprite)
		{
			//Debug.Log("tmp.color" + tmp.color);
			var tmpColor = tmp.color;
			tmpColor.a = val;
			tmp.color = tmpColor;
		}

		foreach (var tmp in AlphaTargetUILabel) {
			var tmpColor = tmp.color;
			tmpColor.a = val;
			tmp.color = tmpColor;
		}

	}
	/*
	//PlayButtonを押されたら、フェードで消える
	public void PlayStartNGUIAlphaAnim(GameObject target, float animTime)
	{

		iTween.ValueTo (
			gameObject,
			iTween.Hash (
			"from",		1,
			"to",		0,
			"time",		animTime,
			"onupdate",	"updateAlphaAnim",
			"easetype",	iTween.EaseType.linear,
			//"oncomplete", "onCompleteHandler",
			"oncompletetarget", gameObject 
			)
		);
	}*/

	/*
	public void RePlayStartNGUIAlphaAnim(GameObject target, float animTime)
	{
		//Debug.Log("target +++++++++++ " + target);
		AlphaTargetUISprite = target.GetComponentsInChildren<UISprite> ();
		AlphaTargetUILabel = target.GetComponentsInChildren<UILabel> ();

		//Debug.Log("animTime +++++++++++ " + animTime);
		iTween.ValueTo (
			gameObject,
			iTween.Hash (
			"from",		1,
			"to",		0,
			"time",		animTime,
			"onupdate",	"updateAlphaAnim",
			"easetype",	iTween.EaseType.linear,
			"oncomplete", "RePlayOnCompleteHandler",
			"oncompletetarget", gameObject
			)
		);
	}
	
	
	void RePlayOnCompleteHandler()
	{
		Debug.Log("RePlayStartAlphaFlag +++++++++++ " + RePlayStartAlphaFlag);
		RePlayStartAlphaFlag = true;
	}
	*/
	/*
	public void WallAlphaAnimFadeIn(GameObject target, float animTime)
	{
		//Debug.Log("WallAlphaAnimFadeIn");
		//updateWallAlphaAnimFade(1);
		iTween.ValueTo (
			gameObject,
			iTween.Hash (
			"from",		0,
			"to",		1,
			"time",		animTime,
			"delay",	3.5f,
			"onupdate",	"updateWallAlphaAnimFade",
			"easetype",	iTween.EaseType.linear
			)
			);
		
	}
	*/
	/*
	public void WallAlphaAnimFadeOut(GameObject target, float animTime)
	{
		Debug.Log("WallAlphaAnimFadeOut");
		//updateWallAlphaAnimFade(0);
		iTween.ValueTo (
			gameObject,
			iTween.Hash (
			"from",		1,
			"to",		0,
			"time",		animTime,
			//"delay",    5,
			"onupdate",	"updateWallAlphaAnimFade",
			"easetype",	iTween.EaseType.linear
			)
			);

	}

	void updateWallAlphaAnimFade(float val)
	{
		for(int i = 0; i < UpperWall.Length; i++){
			wallAlpha =  UpperWall[i].renderer.material.color;
			wallAlpha.a = val;
			UpperWall[i].renderer.material.color = wallAlpha;
		}
	}
	*/
	/*
	public void BGAlphaAnimFadeIn(GameObject target, float animTime)
	{
		//updateWallAlphaAnimFade(1);
		iTween.ValueTo (
			gameObject,
			iTween.Hash (
			"from",		1,
			"to",		0,
			"time",		animTime,
			"onupdate",	"BGWallAlphaAnimFade",
			"easetype",	iTween.EaseType.linear
			)
			);
		
	}

	void BGWallAlphaAnimFade(float val)
	{
		Color L_BlackWallColor = L_BlackWall.renderer.material.color;
		L_BlackWallColor.a = 255;
		Color R_BlackWallColor = R_BlackWall.renderer.material.color;
		R_BlackWallColor.a = 255;
		
		Debug.Log(R_BlackWallColor.a );
		
		Debug.Log("Color L_BlackWallColor");
		R_BlackWall.renderer.material.color = R_BlackWallColor;
		L_BlackWall.renderer.material.color = L_BlackWallColor;
	}
	*/

}
